//
//  EARegisterViewController.m
//  WhatToEat
//
//  Created by Mac on 15/3/30.
//  Copyright (c) 2015年 Mac. All rights reserved.
//

#import "EARegisterViewController.h"
#import "CompleteTableController.h"

@interface EARegisterViewController ()

@end

@implementation EARegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self updateUI];
}



-(void)updateUI
{
    

    UIBarButtonItem *bt=[[UIBarButtonItem alloc] init];
    bt.title=@"返回";
    self.navigationItem.backBarButtonItem=bt;
    
    self.navigationItem.title=@"注册用户";
    
    
    
    self.registerBtn.layer.cornerRadius=kCornerRadius;
    self.registerBtn.clipsToBounds=YES;
    
    
    

    self.registerBtn.backgroundColor=kTingColor;
    
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)strin
{
    
    
    for (UIView *v in textField.superview.subviews) {
        if ([v isKindOfClass:[UIImageView class]]&&v.tag==textField.tag) {
//            v.hidden=YES;
        }
        
        
        
        
        
        
    }
    
    
    
    
    return YES;
}

- (IBAction)registerAction:(UIButton *)sender
{
    
    if (self.userTF.text.length==0) {
        [SVProgressHUD showImage:nil status:@"请输入用户名"];
        return;
    }
    


    
    if (self.aPasswordTF.text.length==0) {
        [SVProgressHUD showImage:nil status:@"请输入密码"];
        
        return;
        
    }
    
    if (self.userTF.text.length<2) {
    
        [SVProgressHUD showImage:nil status:@"用户名不能少于2个字符"];
        return;
    }

    

    
    

    

    
    [WebAPI webAPISignAddMd5tWithNote:@"register/reg" parm:@{
                                                             @"password":self.aPasswordTF.text,
                                                             @"username":self.userTF.text

                                                                 
                                                                 } completed:^(NSInteger state, AFHTTPRequestOperation *operation, id responseObject) {
        

                                                                     
                                                                     NSString *msg=[NSString stringWithFormat:@"%@",responseObject[@"code"]];

                                                                  
                                                                     
                                                                     
                                                                     
                                                                     if ([msg isEqualToString:@"1"]) {
                                                                         
                                                                         
                                                                            [SVProgressHUD showImage:nil status:@"注册成功"];
                                                            
                                                                         
                                                                         
                                                                         
                                                                         ApplicationDelegate.userId=responseObject[@"data"][@"userID"];
                                                                         
                                                                         
                                                                             CompleteTableController * cvc=[self.storyboard instantiateViewControllerWithIdentifier:@"CompleteTableController"];
                                                                         
                                                                             [self.navigationController pushViewController:cvc animated:YES];
                                                                     }else
                                                                     {
                                                                     
                                                                         NSString *errorString=responseObject[@"error"];
                                                                         
                                                                        [SVProgressHUD showImage:nil status:errorString];
                                                                     }
                                                                     
                                                                     
        
                             
        
    }];
    
    
    
//    ApplicationDelegate.userDictionary=[@{
//                                         @"userName":self.userTF.text,
//                                         @"passWord":self.aPasswordTF.text
//                                         
//                                         
//                                         } mutableCopy];
//    
//    
//
//    CompleteTableController * cvc=[self.storyboard instantiateViewControllerWithIdentifier:@"CompleteTableController"];
//    
//    [self.navigationController pushViewController:cvc animated:YES];
    
    

    
    
}

-(void)viewDidDisappear:(BOOL)animated
{

    [super viewDidDisappear:animated];
    
    
    NSLog(@"%@--------%@",self,self.navigationController)



}

- (IBAction)tapAction:(UIControl *)sender
{
    
    
    for (UIView *v in self.view.subviews) {
        if ([v isKindOfClass:[UITextField class]]) {
            
            UITextField * v1=(UITextField *)v;
            
            [v1 resignFirstResponder];
            
        }
        
        
        
    }
}
@end
